﻿<?php //action 3?>
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
<?php //check if GET set
	if (!isset($_GET['userid'])) {
		$to = "client.php";
		redirect_to($to);
		exit;  
		};
 ?>

 <?php //check if user has primary photo

	$query = "SELECT *";
	$query .= " FROM `foto`";
	$query .= " WHERE `foto`.userid =";
	$query .= "'".$_GET['userid']."'";
	$query .= " AND `pagrindine` = 1";
	//echo $query."</br>";
	$result_set = mysql_query($query, $connection);
	$subject = mysql_fetch_array($result_set);
	
	if (($subject['pagrindine']) !=0) {
	//$message = "Pagrindine foto jau yra";
	$pagridine_yra = 1;
	$foto_location = $subject['path'];
	$foto_id = $subject['fotoid'];
	
	} else {
	//$message = "Pagrindines foto nera";
	$pagridine_yra = 0;
	};
	
	
?>

<?php //Procesinam ka gavom
if (isset($_GET['migracijaid'])){
//gaunam duomenys apie migracijos irasha
	$query = "SELECT *";
	$query .= " FROM `migracija`";
	$query .= " WHERE `migracija`.`migracijaid` =";
	$query .= $_GET['migracijaid'];
	$query .= " LIMIT 1";
	
	
	$result_set = mysql_query($query, $connection);
	$result_count = mysql_affected_rows($connection);
//tikrinam ar yra toks migracijos irashas	
	if ($result_count ==1){
		//jamam gautus duomenys apie migracijos irasha
		$migracija = mysql_fetch_array($result_set);
		
		$tolovosid =$migracija['tolovosid'];
		$svarstdata=$migracija['kelimodata'];
		//jamam burys,brigada,lova i kur turejo but perkeltas
		$query = "SELECT *";
		$query .= " FROM `lovos`";
		$query .= " WHERE `lovos`.`lovosid` =";
		$query .= $tolovosid;
		$query .= " LIMIT 1";
		$result_set = mysql_query($query, $connection);
		$vieta = mysql_fetch_array($result_set);
		$toburys=$vieta['burys'];
		$tobrigada=$vieta['brigada'];
		$tolova=$vieta['lova'];
	}else{
		echo "SABOTAGE";
	};
	if (($migracija['userid']) != ($_GET['userid'])){
		echo "SABOTAGE";
	};
};
	
	if (isset($_POST['Submit'])AND(isset($_GET['migracijaid']))) {
		//Esamo redagavimas
		//atnaujinti tolovos id kintamieji
		$burys = ($_POST['burys']);
		$brigada = ($_POST['brigada']);
		$lova = ($_POST['lova']);
		$fromlovosid = ($_POST['fromlovosid']);
		//tikrinam ar ne uzimta lova
			//ieskom lovos id
			$query = "SELECT *";
			$query .= " FROM `lovos`";
			$query .= " WHERE `lovos`.burys =";
			$query .= "'".$burys."'";
			$query .= " AND `lovos`.brigada =";
			$query .= "'".$brigada."'";
			$query .= " AND `lovos`.lova =";
			$query .= "'".$lova."'";
			
			
		
			
			$result_set = mysql_query($query, $connection);
			$subject = mysql_fetch_array($result_set);
			//gaunam lovos id
			$tolovosid = $subject['lovosid'];
			$message .= "lovos id:" .$tolovosid . "</br>";
			
			//ieskom ar nera ko nors toj lovosid
			$query = "SELECT *";
			$query .= " FROM `user`";
			$query .= " WHERE `user`.lovosid =";
			$query .= "'".$tolovosid."'";
			$query .= " AND `user`.`laisvei` = 0";
			echo $query;
			$result_set = mysql_query($query, $connection);
			$subject = mysql_num_rows($result_set);
			echo "    found rows:" . $subject;
			
			
			if ($subject >= 1) {
				//echo "lova uzimta";
				$kas = mysql_fetch_array($result_set);
				//print_r($kas);
				if ($kas['etapas'] == 1){
					$message .= "<strong>Pasirinktoi lovoi jau gyvena: ";
					$message .= $kas['vardas'];
					$message .= " ";
					$message .= $kas['pavarde'];
					$message .= "</strong></br>";
					$message .= "<strong>Bet jis <u>ETAPE</u><strong>   <a href=\"edit_burys.php?userid=";
					$message .= $kas['userid'];
					$message .= "\">Iššokdint?</a></br></br>";
				} else {	
					$message = "<strong>Pasirinktoi lovoi jau gyvena: ";
					$message .= $kas['vardas'];
					$message .= " ";
					$message .= $kas['pavarde'];
					$message .= "</strong></br>";
				};	
			} else {
				//echo "lova laisva";
				$sql = "UPDATE `migracija` SET ";
				$sql .= "kelimodata=";
				$sql .= "'".($_POST['kelimodata'])."'";
				$sql .= ", fromlovosid=";
				$sql .= $fromlovosid;
				$sql .= ", tolovosid=";
				$sql .= $tolovosid;
				
				$sql .= ", perkeltas=";
				if (isset($_POST['perkeltas'])){
					$sql .= "1";
				}else{
					$sql .= "0";
				};
				$sql .= " WHERE `migracija`.`migracijaid` =";
				$sql .= $_GET['migracijaid'];
				echo $sql;
				$result = mysql_query($sql, $connection);
					if (!$result) {
						// Display error message.
						$message .= "<p>Migracija table update failed</p>";
						$message .= "<p>" . mysql_error() . "</p>";
					}else{
						$to = "form_prekelimai.php";
						redirect_to($to);
					};
			};
		
	}else{
		//Naujas
		if (isset($_POST['Submit'])){
			$burys = ($_POST['burys']);
			$brigada = ($_POST['brigada']);
			$lova = ($_POST['lova']);
			$fromlovosid = ($_POST['fromlovosid']);
			
			//echo "POST IS";
		
			//tikrinam ar ne uzimta lova
			$query = "SELECT *";
			$query .= " FROM `lovos`";
			$query .= " WHERE `lovos`.burys =";
			$query .= "'".$burys."'";
			$query .= " AND `lovos`.brigada =";
			$query .= "'".$brigada."'";
			$query .= " AND `lovos`.lova =";
			$query .= "'".$lova."'";
			
			//echo $query;
		
			
			$result_set = mysql_query($query, $connection);
			$subject = mysql_fetch_array($result_set);

			$lovosid = $subject['lovosid'];
			//echo "lovos id:" .$lovosid . "</br>";
			
			
			$query = "SELECT *";
			$query .= " FROM `user`";
			$query .= " WHERE `user`.lovosid =";
			$query .= "'".$lovosid."'";
			//echo $query;
			$result_set = mysql_query($query, $connection);
			$subject = mysql_num_rows($result_set);
			//echo "    found rows:" . $subject;
			
			
			if ($subject >= 1) {
				//echo "lova uzimta";
				$kas = mysql_fetch_array($result_set);
				//print_r($kas);
				if ($kas['etapas'] == 1){
					$message = "<strong>Pasirinktoi lovoi jau gyvena: ";
					$message .= $kas['vardas'];
					$message .= " ";
					$message .= $kas['pavarde'];
					$message .= "</strong></br>";
					$message .= "<strong>Bet jis <u>ETAPE</u><strong>   <a href=\"edit_burys.php?userid=";
					$message .= $kas['userid'];
					$message .= "\">Iššokdint?</a></br></br>";
				} else {	
					$message = "<strong>Pasirinktoi lovoi jau gyvena: ";
					$message .= $kas['vardas'];
					$message .= " ";
					$message .= $kas['pavarde'];
					$message .= "</strong></br>";
				};	
			} else {
				//echo "lova laisva";
			//jeigu naujas kelimas imetam migracijos irasha i migracija table
				$sql = "INSERT INTO `test`.`migracija` (";
				$sql .= "`userid` ,`kelimodata` ,`fromlovosid` ,`tolovosid`) VALUES (";
				$sql .= ($_GET['userid']);
				$sql .= ", '";
				$sql .= ($_POST['kelimodata']);
				$sql .= "', ";
				$sql .= $fromlovosid;
				$sql .= ", ";
				$sql .= $lovosid;
				$sql .= ");";
				
				$result = mysql_query($sql, $connection);
					if (!$result) {
						// Display error message.
						$message .= "<p>Migracija table update failed</p>";
						$message .= "<p>" . mysql_error() . "</p>";
					};
				
			
						
			};
		};
	};

?>

					<article>
						<h2>Burio redagavimas</h2>
						<p>Pildyk forma</p>
						<?php echo $message; ?>
						
	<?php //pagrindiniai kliento duomenys
	//query is user
	$query = "SELECT *";
	$query .= " FROM `user`";
	$query .= " WHERE `user`.userid =";
	$query .= "'".$_GET['userid']."'";
	$result_set = mysql_query($query, $connection);
	$subject = mysql_fetch_array($result_set);

	//query is lovos table
	$query = "SELECT *";
	$query .= " FROM `user`, `lovos`";
	$query .= " WHERE";
	$query .= "`lovos`.`lovosid` = `user`.`lovosid`";
	$query .= " AND";
	$query .= " `user`.userid =";
	$query .= "'".$_GET['userid']."'";
	//$query .= " AND `lovos`.`lovosid` = `user`.`lovosid`";
	//$query .= " AND (`foto`.`userid` = `user`.`userid` AND `foto`.`pagrindine` = 1)";
	//echo $query."</br>";
	$result_set = mysql_query($query, $connection);
	
	$vieta = mysql_fetch_array($result_set);
	
	
	
	//echo "Vardas: ";
	echo $subject['vardas'];
	echo ' ';
	
	// echo "Pavarde: ";
	echo $subject['pavarde'];
	echo ' ';	
	
	//echo "Bylos Nr.: ";
	echo $subject['bylosnr'];
	echo ' Vieta: ';
	
	echo $vieta['burys'] . " - " . $vieta['brigada'] . " (" . $vieta['lova'] . ")";
	echo '</br>';
	
?>
    <?php //rodom pagrindine foto jeigu yra
	if ($pagridine_yra == 1){ 
		echo "</br> <img src=\"";
		echo $foto_location;
		echo "\" width=\"150\">";
	};
?> 
   	<p>Bus perkeltas į:</p>				
						
						
					  <form action="edit_burys.php?userid=<?php echo ($_GET['userid']); 
															if (isset($_GET['migracijaid'])){
															echo "&migracijaid=".($_GET['migracijaid']);
															};?>" method="post" name="burys">
					    <p>
					    </p>
					    <table width="340px" border="1">
					      <tr>
					        <td width="70"> Burys</td>
					        <td width="50"><input name="burys" type="text" value="<?php //jeigu naujas, tai turetu but tuscias
							echo $toburys;
							?>" size="5" required /></td>
					        <td width="60">Brigada</td>
					        <td width="50"><input name="brigada" type="text" value="<?php
							echo $tobrigada;
							?>" size="5" required /></td>
					        <td width="60">Lova</td>
					        <td width="50"><input name="lova" type="text" value="<?php
							echo $tolova;
							?>" size="5" required /></td>
				          </tr>
						  <tr>
						  <td width="70">Perkelimo data:</td>
					        <td colspan="2" ><input name="kelimodata" type="date" value="<?php if (isset($svarstdata)){echo $svarstdata;}else {echo date("Y-m-j");} ?>" size="10" placeholder="1900-12-31" required/></td>
					        <td colspan="2" >Perkeltas</td>
					        <td><input type="checkbox" name="perkeltas" checked></td>
				          </tr>
				        </table>
					    <input name="fromlovosid" type="hidden" value="<?php echo $subject['lovosid'];?>" />
					    <p>
					      <input name="Submit" type="submit" />
					    </p>
					  </form>

			
			</article>
            
<?php require("includes/footer.php"); ?>